package com.bjb.vr.operation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjb.vr.common.ao.OptCusDevAo;
import com.bjb.vr.common.ao.ResDeviceAo;
import com.bjb.vr.common.dto.ResDevice;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.OptCusDevService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * @version 1.0.0
 * @description:客户配置管理-设备管理
 * @author: caihaibo
 * @createDate: 2022/6/17
 */
@RestController
@RequestMapping("/api/operation/v1")
public class CusDevController {

    @Autowired
    OptCusDevService optCusDevService;


    /**
     * 客户配置管理-设备组管理-获取设备组设备列表接口
     *
     * @param cusDevAo
     * @return
     */
    @PostMapping("/queryGroupDevList")
    public BaseResult<IPage<ResDevice>> queryGroupDevList(@RequestBody OptCusDevAo cusDevAo){
        return CommonResult.Ok().setResult(optCusDevService.queryGroupDevList(cusDevAo));
    }

    /**
     * 客户配置管理-设备组管理-增加设备
     *
     * @param cusDevAo
     * @return
     */
    @PostMapping("/addGroupDev")
    public BaseResult addGroupDev(@RequestBody OptCusDevAo cusDevAo){
        boolean success = optCusDevService.addGroupDev(cusDevAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok();
    }

    /**
     * 客户配置管理-设备组管理-修改设备名
     *
     * @param cusDevAo
     * @return
     */
    @PostMapping("/updateGroupDev")
    public BaseResult updateGroupDev(@RequestBody OptCusDevAo cusDevAo){
        Assert.notNull(cusDevAo.getDevId(), "设备Id不能为空！");
        boolean success = optCusDevService.updateGroupDev(cusDevAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * 客户配置管理-设备组管理-删除设备
     *
     * @param cusDevAo
     * @return
     */
    @PostMapping("/deleteGroupDev")
    public BaseResult deleteGroupDev(@RequestBody OptCusDevAo cusDevAo){
        boolean success = optCusDevService.deleteGroupDev(cusDevAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_REMOVE);
        }
        return CommonResult.Ok();
    }


    /**
     * 设备组管理-批量导入设备
     * @param file
     * @return
     */
    @PostMapping("/importBatchDev")
    public BaseResult importBatchDev(@RequestParam("tenantId") String tenantId,@RequestParam("file") MultipartFile file) throws IOException {
        Assert.notNull(tenantId, "多租户Id不能为空！");
        boolean success = optCusDevService.importBatchDev(tenantId,file);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }



    /**
     * 客户配置管理-新增设备
     * @param  resDeviceAo
     * @return
     */
    @PostMapping("/addTenantSingleDev")
    public BaseResult<Boolean>  addTenantSingleDev(@RequestBody ResDeviceAo resDeviceAo){
        boolean success = optCusDevService.addTenantSingleDev(resDeviceAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok();
    }


    /**
     * 获取租户设备列表（资源授权管理）
     *
     * @param resDeviceAo
     * @return
     */
    @PostMapping("/queryTenantDevList")
    public BaseResult<IPage<ResDevice>> queryTenantDevList(@RequestBody ResDeviceAo resDeviceAo){
        Assert.notNull(resDeviceAo.getTenantId(), "多租户Id不能为空！");
        return CommonResult.Ok().setResult(optCusDevService.queryTenantDevList(resDeviceAo));
    }

}
